package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.AccountLockedException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishListVo;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 添加菜品
     * @param dishDTO
     */
    @Override
    @Transactional
    public void addDishWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setStatus(StatusConstant.ENABLE);

        dishMapper.add(dish);
        Long id = dish.getId();

        // 这边的dishID看Mapper
        List<DishFlavor> dishFlavorList = dishDTO.getFlavors();
        if(dishFlavorList != null && dishFlavorList.size() > 0) {
            dishFlavorList.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);
            });
            dishFlavorMapper.addBatch(dishFlavorList);
        }
    }

    /**
     * 条件，分页查询菜品的信息
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult getDishWithFlavor(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishListVo> dishPage = dishMapper.getDishWithConditions(dishPageQueryDTO.getName(), dishPageQueryDTO.getCategoryId(), dishPageQueryDTO.getStatus());
        return new PageResult(dishPage.getTotal(), dishPage.getResult());
    }

    /**
     * 删除菜品和口味
     * @param ids
     */
    @Override
    @Transactional
    public void deleteDish(List<Long> ids) {

        List<Long> statusNoArrow = dishMapper.getByStatusAndIds(ids, StatusConstant.ENABLE);
        if(statusNoArrow.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }

        List<Long> idsArrays = setmealDishMapper.countByDishId(ids);
        if(idsArrays != null && idsArrays.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        dishMapper.deleteDishByIds(ids);

        dishFlavorMapper.deleteDishFlavorByIds(ids);

    }

    /**
     * 根据id获取菜品详细信息
     * @param id
     * @return
     */
    @Override
    public DishVO getDishById(Long id) {
        return dishMapper.getDishById(id);
    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDTO dishDTO) {

        List<Long> id = new ArrayList<>();
        id.add(dishDTO.getId());
        dishFlavorMapper.deleteDishFlavorByIds(id);

        Long dishId = dishDTO.getId();
        List<DishFlavor> dishFlavorList = dishDTO.getFlavors();
        if(dishFlavorList != null && dishFlavorList.size() > 0) {
            dishFlavorList.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishFlavorMapper.addBatch(dishFlavorList);
        }

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);

    }

    /**
     * 根据id修改菜品状态
     * @param status
     * @param id
     */
    @Override
    public void uodateDishStatusById(Integer status, Long id) {

        Dish dish = Dish.builder()
                .status(status)
                .id(id)
                .build();

        dishMapper.update(dish);
    }

    /**
     * 通过菜品id查询分类列表
     * @param dishDTO
     * @return
     */
    @Override
    public List<DishVO> getDishWithFlavorByCategoryId(DishDTO dishDTO) {
        return dishMapper.getDishWithFlavorByCategoryId(dishDTO.getCategoryId());
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> getDishByCategoryId(Long categoryId) {
        return dishMapper.getDishByCategoryId(categoryId);
    }
}
